﻿using Autofac;
using Biotech.Log;
using project.B12.application;
using project.B12.application.Ioc;
using project.B12.application.Views;
using project.B12.IService;
using project.B12.Service;
using project.B12.Service.UserService;
using project.B12.Views;
using project.B12.Views.Dialog;
using software.Common.MVVM;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Linq;

namespace project.B12.ViewModels
{
    public class MainViewModel  : ObservableObject
    {
        #region 资源引用
        public IUserService userService { get; set; }
        public PublicResourceViewModel publicResourceViewModel { get; set; }
        public IServiceDevice serviceDevice { get; set; }
        #endregion

        #region Command
        public RelayCommand LogOutCommand => new(LogOut);//注销用户
        public RelayCommand AboutCommand => new(About);//关于
        public RelayCommand MinimizeScreenCommand => new(MinimizeScreen);//最小化窗口
        public RelayCommand FullScreenCommand => new(FullScreen);//全屏切换窗口
        public RelayCommand ExitSystemCommand => new(ExitSystem);//退出系统
        #endregion Command

        #region Model
        private FrameworkElement currentMonitorView;//当前主窗体框架显示的监控视图
        private FrameworkElement currentAnalyseView;//当前主窗体框架显示的分析视图
        private FrameworkElement currentSettingView;//当前主窗体框架显示的设置视图

        public FrameworkElement CurrentMonitorView//当前主窗体框架显示的监控视图
        {
            get { return currentMonitorView; }
            set { currentMonitorView = value; this.RaisePropertyChanged(); }
        }
        public FrameworkElement CurrentAnalyseView//当前主窗体框架显示的分析视图
        {
            get { return currentAnalyseView; }
            set { currentAnalyseView = value; this.RaisePropertyChanged(); }
        }
        public FrameworkElement CurrentSettingView//当前主窗体框架显示的设置视图
        {
            get { return currentSettingView; }
            set { currentSettingView = value; this.RaisePropertyChanged(); }
        }
        #endregion Model
        public MainViewModel(IUserService _userService,IServiceDevice _serviceDevice)
		{
            userService= _userService;
            publicResourceViewModel = ViewModelLocator.PublicResourceViewModel;
            serviceDevice= _serviceDevice;
            CreateView();

        }

        /// <summary>
        /// 初始化视图
        /// </summary>
        public void CreateView()
        {
            CurrentMonitorView = new MonitorView();
            CurrentAnalyseView = new AnalyseView();
            CurrentSettingView = new SettingView();
        }

        /// <summary>
        /// 注销用户切换登录界面
        /// </summary>
        /// <param name="obj"></param>
        private async void LogOut()
        {
            try
            {
                if (await MessageDialogView.ComfirmMessage("注销用户", $"请确认是否注销当前用户？", "Main"))
                {
                    ViewModelLocator.LoginViewModel.IsLogOut= true;
                    LoggerFactory.Instance.CurrentLogger.LogInfo($"注销登录\n①用户名：({userService.CurrentUser.Name})\n②角色名：({userService.CurrentUser.RoleName})", "", "用户");
                    IocContainer.Instance.Resolve<MainWindowViewModel>().ChangeToLoginView();//转登录界面
                }
            }
            catch (Exception ex)
            {
                WPF.Controls.Message.Push($"未知错误：{ex}", MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 关于窗口
        /// </summary>
        /// <param name="parameter">无效参数</param>
        private void About()
        {
            try
            {
                App.Current.Dispatcher.Invoke((Action)(() =>
                {
                    AboutView.ComfirmAbout(ViewModelLocator.PublicResourceViewModel.SoftwareName,System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString(), "Main");
                }));
            }
            catch (Exception ex)
            {
                WPF.Controls.Message.Push($"未知错误：{ex}", MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 最小化窗口
        /// </summary>
        /// <param name="parameter">无效参数</param>
        private void MinimizeScreen()
        {
            try
            {
                ViewModelLocator.PublicResourceViewModel.MinimizeScreen();
            }
            catch (Exception ex)
            {
                WPF.Controls.Message.Push($"未知错误：{ex}", MessageBoxImage.Error);
            }
}

        /// <summary>
        /// 全屏切换窗口
        /// </summary>
        /// <param name="parameter">无效参数</param>
        private void FullScreen()
        {
            try
            {
                ViewModelLocator.PublicResourceViewModel.FullScreen();
            }
            catch (Exception ex)
            {
                WPF.Controls.Message.Push($"未知错误：{ex}", MessageBoxImage.Error);
            }
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <param name="parameter">无效参数</param>
        private async void ExitSystem()
        {
            if (await MessageDialogView.ComfirmMessage("退出系统", $"请确认是否退出系统？", "Main"))
            {
                try
                {
                    LoggerFactory.Instance.CurrentLogger.LogInfo("软件关闭", "", "软件");
                    Environment.Exit(0);
                }
                catch (Exception ex)
                {
                    System.Windows.MessageBox.Show("系统未正常关闭！错误信息：" + ex);
                }
            }
        }
    }
}